package com.lance.answer.question_answer;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.util.Constant;
import com.lance.answer.util.HttpClientUtil;
import com.lance.answer.util.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

;

@Slf4j
public class QuestionAnswer_new {

    //此处设置并发执行线程数量
    private static int sync = 8;

    /**
     * 进度表：
     * 6-18 19:55开始测试 公共行政学 行政管理专科
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        while (LocalDateTime.now().getHour() >= 3 && LocalDateTime.now().getHour() < 5) {
            log.info("当前时间：{} 国开网维护中 程序暂时休眠20min。。。", LocalDateTime.now().toString());
            HttpClientUtil.send("当前时间：" + LocalDateTime.now().toString() + "国开网维护中 程序暂时休眠20min....");
            TimeUnit.MINUTES.sleep(20);
        }
        //     HttpClientUtil.send("当前时间：" + LocalDateTime.now().toString() + "国开网维护结束 程序恢复运行....");

        //并发开启的浏览器数量
        int sync = 8;
        //是否提交答案
        boolean commit = true;
//        String majorName = "行政管理专科2103";
//        String subjectName = "人力资源管理";
        String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";
        String majorName = "老生刷课1117";
        //是否推送微信消息
        boolean push = false;
        //驱动路径
        String driverPath = "libs/chromedriver.exe";
        //初始化日志配置
        Constant.initLog();
        StringBuilder sb = new StringBuilder();
        sb
                .append("刷题课程")
                .append(" : ")
                .append(subjectName)
                .append("\n")

                .append("刷题专业")
                .append(" : ")
                .append(majorName)
                .append("\n")

                .append("并发开启浏览器")
                .append(" : ")
                .append(sync)
                .append("\n")

                .append("是否提交答案")
                .append(" : ")
                .append(commit)
                .append("\n");
        String message = sb.toString();
        System.out.println(message);
        if (push) {
            HttpClientUtil.send(message);
        }
        start(subjectName, majorName, sync, driverPath, commit, push);
    }

    public static void start(String subjectName, String majorName, int sync, String driverPath, boolean commit, boolean push) throws Exception {
        //获取模板路径,通过流的方式获取文件可以避免被打成jar包之后无法获取文件路径的异常
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");
        String answerStr = IOUtils.toString(inputStream);

        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> map = new HashMap<>();
        for (AnswerObject answerObject : list) {
            map.put(answerObject.getQuestionCode(), answerObject);
        }

        InputStream inputStream2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + "users/" + majorName + ".user");
        List<String> users = IOUtils.readLines(inputStream2, "utf-8");

        InputStream inputStream3 = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-quiz.section");
        List<String> list1 = IOUtils.readLines(inputStream3);
        List<String> paths = new ArrayList<>();
        for (String str : list1) {
            paths.add(str.split(",")[0].trim());
        }

        log.info("本次加载用户：{} 人", users.size());
        //log.info("本次程序将加载 {} 个线程同时运行", group.size());

        for (String user:users) {
            Constant.USER_QUEUE.putLast(user);
        }
        Constant.SUM = users.size();
        StringBuilder sb2 = new StringBuilder();
        sb2
                .append("本次加载用户： " + Constant.USER_QUEUE.size() + "人")
                .append("\n")

                .append("每个用户需刷：" + paths.size() + " 个测试题")
                .append("\n");
        String message2 = sb2.toString();
        System.out.println(message2);
        if (push) {
            HttpClientUtil.send(message2);
        }

        for (int i = 1; i <= sync; i++ ) {
            Thread answerThread = new AnswerTask(paths, map, subjectName, driverPath, Constant.USER_QUEUE, commit, push);
            answerThread.setName("刷题线程--" + i);
            answerThread.start();
        }

    }
}
